package com.doradance.app.home;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.doradance.R;
import com.doradance.account.utils.FragmentUtils;
import com.doradance.app.appactivity.message.MessageDetailActivity;
import com.doradance.app.appactivity.search.SearchActivity;
import com.doradance.entity.HideHomeRedEntity;
import com.doradance.entity.HideRedEntity;
import com.doradance.entity.refreshEntity;
import com.pachong.android.baseuicomponent.fragment.BaseFragment;
import com.pachong.android.baseuicomponent.fragment.LoadableFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by Zoi.
 * E-mail：KyluZoi@gmail.com
 * 2016/12/21
 */

public class HomeFragment extends LoadableFragment {

    @Bind(R.id.iv_home_red)
    ImageView mIvHomeRed;

    public HomeFragment() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        EventBus.getDefault().register(this);
        FragmentUtils.replaceFragment(getChildFragmentManager(), R.id.dro_home, new HomeSubFragment());
        ButterKnife.bind(this, rootView);
        return rootView;
    }


    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.frag_home, container, false);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(refreshEntity refreshEvent){
        mIvHomeRed.setVisibility(View.VISIBLE);
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(HideHomeRedEntity refreshEvent){
        mIvHomeRed.setVisibility(View.GONE);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.iv_home_message, R.id.iv_home_vip, R.id.et_home_message})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_home_message:
                MessageDetailActivity.start(getActivity());
                mIvHomeRed.setVisibility(View.GONE);
                EventBus.getDefault().post(new HideRedEntity());
                break;
            case R.id.iv_home_vip:
                RankLearnActivity.start(getActivity());
                break;
            case R.id.et_home_message:
                SearchActivity.start(getActivity());
                break;
        }
    }

    @Override
    public void onStartLoading() {
        EventBus.getDefault().register(this);
        FragmentUtils.replaceFragment(getChildFragmentManager(), R.id.dro_home, new HomeSubFragment());
    }
}
